using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.StateMachine.SPI;

namespace T.FAS.Runtime.StateMachine.Engine
{
    internal class CopyActionProcess : IActionProcess
    {
        public const string CSTKEY = "Copy";

        private List<ParamInfo> paramList = new List<ParamInfo>();

        public CopyActionProcess()
        {
            paramList.Add(new ParamInfo() { Name = "FromTarget", IsRequired = true, Description = "复制源信息" });
        }

        public string Key => CSTKEY;

        public string Description => "将目标列的值复制到当前对象";

        public List<ParamInfo> NeedParamInfo { get { return paramList; } }

        public TRecord Do(TRecord record, TAction action, StateMachineMetadata metadata, Dictionary<string, object> extParams = null)
        {
            if (record == null || action == null || string.IsNullOrEmpty(action.Target))
                return record;
            Dictionary<string, object> paramters = action.Paramters;
            if (paramters == null || paramters.ContainsKey("FromTarget") == false)
                throw new Exception(CommonStringClass.CopyActionProcess_Do);
            var from = Convert.ToString(paramters["FromTarget"]);
            var target = action.Target;
            if (record.GetDynamicMemberNames().Contains(target, StringComparer.OrdinalIgnoreCase) == false)
                throw new Exception(CommonStringClass.CopyActionProcess_Do1+ target);

            var data = record.GetValue(from);
            record.SetValue(target, data);
 
            return record;
        }
    }
}
